//TestScriptBlock.block
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(com.blocks.script);
importPackage(com.jme3.math);
importPackage(com.jme3.collision);
importPackage(com.jme3.scene.debug);
importPackage(com.jme3.scene);
importPackage(com.jme3.material);

var block = new Object();
var counter = 0;

var direction = 0;
var rayHit = false;
var mark;
block.setup = function(name) {
	thisBlock.setName("RangeFinder");
	thisBlock.setTextureRow(1.0);
	thisBlock.setTextureColumn(2.0);
	thisBlock.resetTexture();
	thisBlock.setCustomFrequency(true);
	thisBlock.getParameters().put("axis", 2);
	block.initMark(50);

}
block.loop = function(name, tpf) {
	var found = false;
	if (thisBlock.isPowered()) {
		results = new CollisionResults();
		var axis = thisBlock.getParameters().get("axis");
		var column = axis % 3;
		direction = thisBlock.getWorldRotation().getRotationColumn(column);
		if (axis > 2) {
			direction = direction.mult(-1);
		}
		ray = new Ray(thisBlock.getWorldTranslation(), direction);
		ScriptBlocksApplication.getInstance().getRootNode().collideWith(ray,
				results);

		var distance = 0;
		if (results.size() > 0) {
			itr = results.iterator();
			while (itr.hasNext()) {
				closest = itr.next();

				if (closest.getGeometry() != null && closest.getDistance() > 1
						&& closest.getDistance() < 50) {
					hitNode = closest.getGeometry().getParent();
					if (ScriptUtils.isBlock(hitNode)) {
						// Block Width Size is 2
						// Ray starts from the middle of the block subtract 1 to
						// get to outside of block
						distance = java.lang.Math.rint(closest.getDistance()) - 1;
						if (distance > 0) {
							distance = distance / 2;
						}
						thisBlock.detachChild(mark);
						block.initMark(closest.getDistance());
						thisBlock.attachChild(mark);
//						ScriptBlocksApplication.getInstance().cameraDirectionText
//								.setText("AXIS"
//										+ thisBlock.getParameters().get("axis")
//										+ "DISTANCE " + distance);
						found = true;
						break;
					}
				}
			}

		}
		thisBlock.setAllFrequency(FastMath.abs(distance));
	}
	if (!found) {
		thisBlock.detachChild(mark);
		block.initMarkWhite();
		thisBlock.attachChild(mark);
	}
}

block.use = function() {
	var axis = new Float(thisBlock.getParameters().get("axis")).intValue(); // This
																			// is
																			// comming
																			// back
																			// as a
																			// string
																			// 2.0
	axis = (axis + 1) % 6;
	thisBlock.getParameters().put("axis", axis);
}

block.scriptExport = function() {
	return "";
}

block.initMark = function(distance) {
	if (mark != null) {
		ScriptBlocksApplication.getInstance().getRootNode().detachChild(mark);
	}
	var axis = thisBlock.getParameters().get("axis");
	var arrow = new Arrow(Vector3f.UNIT_X.mult(distance));
	if (axis == 1) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(distance));
	} else if (axis == 2) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(distance));
	} else if (axis == 3) {
		arrow = new Arrow(Vector3f.UNIT_X.mult(-distance));
	} else if (axis == 4) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(-distance));
	} else if (axis == 5) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(-distance));
	}

	arrow.setLineWidth(3);

	mark = new Geometry("BOOM!", arrow);

	mark_mat = new Material(ScriptBlocksApplication.getInstance()
			.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
	mark_mat.setColor("Color", ColorRGBA.Red);
	mark.setMaterial(mark_mat);
}
block.initMarkWhite = function() {
	if (mark != null) {
		ScriptBlocksApplication.getInstance().getRootNode().detachChild(mark);
	}
	var axis = thisBlock.getParameters().get("axis");
	var arrow = new Arrow(Vector3f.UNIT_X.mult(2));
	if (axis == 1) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(2));
	} else if (axis == 2) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(2));
	} else if (axis == 3) {
		arrow = new Arrow(Vector3f.UNIT_X.mult(-2));
	} else if (axis == 4) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(-2));
	} else if (axis == 5) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(-2));
	}
	arrow.setLineWidth(3);

	mark = new Geometry("BOOM!", arrow);

	mark_mat = new Material(ScriptBlocksApplication.getInstance()
			.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
	mark_mat.setColor("Color", ColorRGBA.White);
	mark.setMaterial(mark_mat);
}
